
package com.jf.cloud.multishop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.product.feign.SpuFeignClient;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.product.constant.SalesType;
import com.jf.cloud.common.vo.ShopSubstituteSalesVO;
import com.jf.cloud.multishop.mapper.ShopSubstituteSalesMapper;
import com.jf.cloud.multishop.model.ShopSubstituteSales;
import com.jf.cloud.multishop.service.ShopSubstituteSalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 代销设置表
 *
 * @author zz
 * @date 2022-06-22 17:09:12
 */
@Service
public class ShopSubstituteSalesServiceImpl implements ShopSubstituteSalesService {

    @Autowired
    private ShopSubstituteSalesMapper shopSubstituteSalesMapper;

    @Autowired
    private SpuFeignClient spuFeignClient;

    @Override
    public PageVO<ShopSubstituteSales> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> shopSubstituteSalesMapper.list());
    }

    @Override
    @Cacheable(cacheNames = CacheNames.SHOP_SUBSTITUTE_SALES_KEY, key = "#shopId")
    public ShopSubstituteSalesVO getByShopSubstituteSalesId(Long shopId) {
        return shopSubstituteSalesMapper.getByShopSubstituteSalesId(shopId);
    }

    @Override
    public void save(ShopSubstituteSales shopSubstituteSales) {
        shopSubstituteSalesMapper.save(shopSubstituteSales);
    }

    @Override
    public void update(ShopSubstituteSales shopSubstituteSales) {
        int count = shopSubstituteSalesMapper.update(shopSubstituteSales);
        RedisUtil.del(CacheNames.SHOP_SUBSTITUTE_SALES_KEY + CacheNames.UNION + shopSubstituteSales.getShopId());
        if (count > 0) {
            //手动设价改成智能设价， 已导入的商品的价格要做更新；智能设价改成手动设价  已导入的商品价格不用更新；
            if (Objects.equals(shopSubstituteSales.getSalesType(), SalesType.SMART_PRICE.value())) {
                spuFeignClient.updateManualPriceCommissionSpuPrice(shopSubstituteSales.getShopId());
            }
        }
    }

    @Override
    public void deleteById(Long shopSubstituteSalesId) {
        shopSubstituteSalesMapper.deleteById(shopSubstituteSalesId);
    }

    @Override
    public List<ShopSubstituteSalesVO> listByShopIds(List<Long> shopIds) {
        if (CollUtil.isEmpty(shopIds)) {
            return new ArrayList<>();
        }
        return shopSubstituteSalesMapper.listByShopIds(shopIds);
    }

}
